package china.lzy.config.interceptor;


import china.lzy.entity.ResCode;
import china.lzy.entity.ResResult;
import china.lzy.exception.AuthErrException;
import china.lzy.exception.BusinessException;
import china.lzy.exception.ParamErrorException;
import china.lzy.exception.SystemException;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 异常处理类
 */
@Log4j2
@ControllerAdvice//控制器增强
public class ExceptionCatch {

    //捕获AuthErrException此类异常
    @ExceptionHandler(AuthErrException.class)
    @ResponseBody
    public ResResult authErrException(Exception exception) {
        exception.printStackTrace();
        //记录日志
        log.error("catch exception:{}", exception.getMessage());
        //返回通用异常
        return ResResult.ResResultBuilder(ResCode.AUTH_ERROR,exception.getMessage());
    }

    //捕获ParamErrorException此类异常
    @ExceptionHandler(ParamErrorException.class)
    @ResponseBody
    public ResResult paramErrorException(Exception exception) {
        exception.printStackTrace();
        //记录日志
        log.error("catch exception:{}", exception.getMessage());
        //返回通用异常
        return ResResult.ResResultBuilder(ResCode.OPERATE_ERROR,exception.getMessage());
    }

    //捕获SystemException此类异常
    @ExceptionHandler(SystemException.class)
    @ResponseBody
    public ResResult systemException(Exception exception) {
        exception.printStackTrace();
        //记录日志
        log.error("catch exception:{}", exception.getMessage());
        //返回通用异常
        return ResResult.ResResultBuilder(ResCode.SYSTEM_ERROR,exception.getMessage());
    }

    //捕获BusinessException此类异常
    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public ResResult businessException(Exception exception) {
        exception.printStackTrace();
        //记录日志
        log.error("catch exception:{}", exception.getMessage());
        //返回通用异常
        return ResResult.ResResultBuilder(ResCode.SYSTEM_BUSY,exception.getMessage());
    }

    //捕获Exception此类异常
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResResult exception(Exception exception) {
        exception.printStackTrace();
        //记录日志
        log.error("catch exception:{}", exception.getMessage());
        //返回通用异常
        return ResResult.ResResultBuilder(ResCode.SYSTEM_ERROR);
    }
}